package cn.diancan.action.manager;


import cn.diancan.action.BaseAction;
import cn.diancan.exception.OutMsgException;
import cn.diancan.manager.security.ManagerAuth;
import cn.diancan.model.SysRole;
import cn.diancan.vo.AuthorityVo;
import com.opensymphony.xwork2.ModelDriven;

import java.util.List;

public class RoleAction extends BaseAction implements ModelDriven<AuthorityVo> {
    private static final long serialVersionUID = 1L;

    private AuthorityVo requestVo = new AuthorityVo();

    /**
     * 角色列表
     */
    @ManagerAuth
    public String roleList() {
	List<SysRole> list = sysRoleService.findAll();
	getRequest().setAttribute("list", list);
	return freemarkView("/views/ftl/manager/role/role_list.ftl");
    }
	
    /**
     * 新增角色界面
     */
    @ManagerAuth
    public String addRoleUI() {
	return freemarkView("/views/ftl/manager/role/role_add.ftl");

    }
	
    /**
     * 新增角色
     */
    @ManagerAuth
    public String addRole() {
		try {
			SysRole bean = new SysRole();
			bean.setName(requestVo.getName());
			bean.setDescription(requestVo.getDescription());
			sysRoleService.saveRole(bean);
			return redirect("/manager/Role_roleList");
		} catch (Exception e) {
			return managerError(e.getMessage());
		}

	}

    /**
     * 编辑角色界面
     */
    @ManagerAuth
    public String editRoleUI() {
	try {
	    SysRole bean = sysRoleService.findById(requestVo.getRoleId());
	    getRequest().setAttribute("role", bean);
	} catch (OutMsgException e) {
	    e.printStackTrace();
	}
	return freemarkView("/views/ftl/manager/role/role_edit.ftl");
    }
	
    /**
     * 编辑角色
     */
    @ManagerAuth
    public String editRole() {
	SysRole bean;
	try {
	    bean = sysRoleService.findById(requestVo.getRoleId());
	    bean.setName(requestVo.getName());
	    bean.setDescription(requestVo.getDescription());
	    sysRoleService.saveRole(bean);
	} catch ( Exception e) {
	    e.printStackTrace();
	}
	return redirect("/manager/Role_roleList"); 
    }
	
    /**
     * 删除角色
     */
    @ManagerAuth
    public String deleteRole() {
	try {
	    sysRoleService.deleteRole(requestVo.getRoleId());
		return jsonSuccess("删除成功");
	} catch ( Exception e) {
	    return jsonError(e);
	}

    }

    @Override
    public AuthorityVo getModel() {
	return requestVo;
    }
	
	
}
